package com.example.perplonews.a_d_fragment.c_video

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.get
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.perplonews.R
import com.example.perplonews.a_b_utils.LjlUtils
import com.example.perplonews.a_d_fragment.c_video.adapter.Small_Adapter
import com.example.perplonews.a_d_fragment.c_video.entity.Data
import com.example.perplonews.a_d_fragment.c_video.entity.SmallVideo
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response


class video2_Fragment : Fragment() {
    private var mview:View?=null
    private val smallRec: RecyclerView by lazy { mview!!.findViewById<RecyclerView>(R.id.small_rec) }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        init()
            mview = inflater.inflate(R.layout.fragment_video2_, container, false)
        return mview
    }

    private fun init() {
        OkGo.get<String>("http://v.juhe.cn/toutiao/index?type=top&key=e3080ff4a57ed090c999dc0d9082dce4").execute(object :StringCallback(){
            override fun onSuccess(response: Response<String>) {
                var body = response.body()

                var fromJson = Gson().fromJson(body, SmallVideo::class.java)
                var data = fromJson.result.data
                var adapter = Small_Adapter(data as ArrayList<Data>)
                smallRec.adapter = adapter
                smallRec.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
                adapter.setOnItemClickListener { adapter, view, position ->
                    adapter.getItem(position)

                }
            }

        })
    }


}